//
//  UIView+FloatingSubView.m
//  F FloatingPlayer
//
//  Created by yonas on 2018/9/26.
//  Copyright © 2018年 Inspur. All rights reserved.
//  暂时借助userdefaults存储

#import "UIView+FloatingSubView.h"
#import "FloatingViewModel.h"
static NSString *originalCacheWidth = @"originalCacheWidth";

CGFloat originalW = 0;
@implementation UIView (FloatingSubView)

- (void)backToPreViewController{
    [[NSNotificationCenter defaultCenter]postNotificationName:BACK_TO_PRE_CONTROLLER object:nil];
}

- (void)floatingHide{
    [[NSNotificationCenter defaultCenter]postNotificationName:HIDE_FLOATING_VIEW object:nil];
}

- (void)changeSizeWithSender:(id)sender event:(UIEvent *)event{
    CGRect r = [ UIScreen mainScreen ].bounds;
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint point = [touch locationInView:self];
    CGPoint prepoint = [touch previousLocationInView:self];
    if (self.frame.size.width >=r.size.width){
        if ((point.x -prepoint.x)>0){
            return;
        }
    }
    if (originalW == 0){
        originalW = self.frame.size.width;
    }
    CGFloat changeToWidth = self.window.frame.size.width + (point.x - prepoint.x);
    CGFloat changeToHeight = 333.0f * changeToWidth / 587.0f;
    if (changeToWidth > originalW) {
        [self.window setFrame:CGRectMake(self.window.frame.origin.x, self.window.frame.origin.y, changeToWidth, changeToHeight)];
        [self setFrame:CGRectMake(0, 0, changeToWidth, changeToHeight)];
        for (CALayer *layer in self.layer.sublayers) {
            [layer setFrame:self.bounds];
        }
    }
}

@end
